package com.example.pingze.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.pingze.entity.ID;
import com.example.pingze.entity.PoemInfo;
import com.example.pingze.entity.PoemInfo1;
import com.example.pingze.repository.PoemFileRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


@Service
public class PoemFileService {
    @Autowired
    private PoemFileRepository repository;
    public List<PoemInfo> fromJson() {
        List<PoemInfo> poemList = new ArrayList<>();
        String json1 = "C:\\Users\\asus\\Desktop\\chinese-gushiwen\\guwen\\11.json";
        File jsonFile = new File(json1);
        String jsonData = getJsonStr(jsonFile);
        JSONArray jsonArray = JSONUtil.parseArray(jsonData);
        List<PoemInfo1> poemInfoList = JSONUtil.toList(jsonArray, PoemInfo1.class);
        for(PoemInfo1 list : poemInfoList){
            PoemInfo poemInfo = getPoemInfoList(list);
            poemList.add(poemInfo);
        }
        return poemList;
    }

    private PoemInfo getPoemInfoList(PoemInfo1 list) {
        PoemInfo poemInfo = new PoemInfo();
            ID id = list.get_id();
            String oid = id.get$oid();
            String title = list.getTitle();
            String dynasty = list.getDynasty();
            String writer = list.getWriter();
            List<String> type = list.getType();
            String type1 = type.toString();
            String content = list.getContent();
            String remark = list.getRemark();
            String appreciation = list.getShangxi();
            String translation = list.getTranslation();
            poemInfo.setId(oid);
            poemInfo.setTitle(title);
            poemInfo.setDynasty(dynasty);
            poemInfo.setWriter(writer);
            poemInfo.setTypes(type1);
            poemInfo.setContent(content);
            poemInfo.setRemark(remark);
            poemInfo.setAppreciation(appreciation);
            poemInfo.setTranslations(translation);
        return poemInfo;
    }

    /**
     * 该方法:将json文件中的数据读取成字符串形式
     * @return
     */
    public String getJsonStr(File file){
        String jsonStr = "";
        try {
            FileReader fileReader = new FileReader(file);
            Reader reader = new InputStreamReader(new FileInputStream(file));
            int ch = 0;
            StringBuffer buffer = new StringBuffer();
            while (((ch = reader.read()) != -1)) {
                buffer.append((char)ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = buffer.toString();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
        return jsonStr;
    }
    public void insertPoemInfo(List<PoemInfo> list) {
        for(PoemInfo poemInfo : list){
            repository.save(poemInfo);
        }
    }
}
